
#ifndef JSON_PARSE_H
#define JSON_PARSE_H

#include <Arduino.h>
#include <String.h> // 注意：Arduino中的String类通常不推荐使用，因为它基于堆，可能导致内存碎片。但为了简化，这里暂时使用它。
#include <ArduinoJson.h>
// 定义Action类（已简化和修改以适应Arduino环境）
#include "utils.h"
#include "NTPTime.h"
#include "config.h"





class Action
{
public:
    String id;
    int on; // 0或1表示关闭或打开
    String name;

    // 构造函数（可选）
    Action(String id, int on_state)
    {
        this->id = String(id);
        this->on = on_state;
    }
    Action()
    {
    }
};

// TimerAction类的嵌套类A的C++版本
class TimerActionA//singleTimeAction
{
public:
    int index; 
    String actionTime;
    String duration;
    Action *action; // 使用指针以避免对象复制，注意内存管理
    bool on = true;
    bool isTriggleVal =false;

    bool isTriggle(){
        return isTriggleVal;
    }

    void setTriggle(bool b){
        this->isTriggleVal = b;
    }


   String getActionTime(){
    return this->actionTime;
   }


    long getActionTimeTimeStamp()
    {
        time_t actionTime = stringToTimestamp(this->actionTime.c_str());
        return actionTime; 
    }



       unsigned long getDuration()
    {

        return hmsString2Sencod(duration.c_str());
    }


    TimerActionA()
    {
    }
    void print()
    {

        Serial.print("actionTime:");
        Serial.print(actionTime);

        Serial.print("duration:");
        Serial.print(duration);

        Serial.print("on:");
        Serial.println(on);
    }
    // 构造函数（可选）
    TimerActionA(const char *action_time, const char *duration, Action *act, bool on_state)
    {
        actionTime = String(action_time);
        this->duration = String(duration);
        this->action = act;
        this->on = on_state;
    }
};

// TimerAction类的嵌套类B的C++版本intervalTime
class TimerActionB
{
public:
    int index;
    String intervalTime;
    String duration;
    Action *action; // 使用指针以避免对象复制，注意内存管理
    bool on = true;
    bool isTriggleVal =false;

    bool isTriggle(){
        return isTriggleVal;
    }

    void setTriggle(bool b){
        this->isTriggleVal = b;
    }

    void setIndex(int index){
        this->index = index;
    }

    int getIndex(){
        return this->index;
    }
     

    unsigned long getDuration()
    {

        return hmsString2Sencod(duration.c_str());
    }

    unsigned long getIntervalTime()
    {

        return dhmString2Sencod(intervalTime.c_str());
    }
    void print()
    {

        Serial.print("    intervalTime:");
        Serial.print(intervalTime);


        Serial.print("    on:");
        Serial.println(on);
    }

    TimerActionB()
    {
    }

    // 构造函数（可选）
    TimerActionB(const char *interval_time, const char *duration, Action *act, bool on_state)
    {
        intervalTime = String(interval_time);
        this->duration = String(duration);
        this->action = act;
        this->on = on_state;
    }
};

// TimerAction类的嵌套类DayItem的C++版本
class DayItem
{
public:
    bool on;
    int weekNumber;            // 0-6代表星期日到星期六
    TimerActionA *actions[10]; // 假设每个DayItem最多有10个TimerActionA，注意内存管理
    int actionCount = 0;       // 实际actions的数量

    // 添加TimerActionA的方法
    void addAction(TimerActionA *act)
    {
        if (actionCount < 10)
        {
            actions[actionCount++] = act;
        }
    }
};

// TimerAction类的C++版本
class TimerAction
{
public:
    DayItem weekItems[7];                // 每周7天的计划
    TimerActionA *singleTimerAction[10]; // 假设最多有10个单次定时任务，注意内存管理
    int singleActionCount = 0;           // 实际singleTimerAction的数量

    TimerActionB *intervalTimerAction[10]; // 假设最多有10个周期定时任务，注意内存管理
    int intervalActionCount = 0;           // 实际intervalTimerAction的数量

    String deviceId;

    // 构造函数（可选）
    TimerAction(const char *device_id)
    {
        deviceId = String(device_id);
    }

    // 添加单次定时任务的方法
    void addSingleTimerAction(TimerActionA *act)
    {
        if (singleActionCount < 10)
        {
            singleTimerAction[singleActionCount++] = act;
        }
    }

    // 添加周期定时任务的方法
    void addIntervalTimerAction(TimerActionB *act)
    {
        if (intervalActionCount < 10)
        {
            intervalTimerAction[intervalActionCount++] = act;
        }
    }
};

// 设备的基本配置
class TypeConfig
{
private:
    String endTime;
    String startTime;
    String frequency_daytime;
    String frequency_night;
    String wakeupInterval;//休眠模式，省电，唤醒间隔
    String wakeupInterval_daytime;//单位秒
     String wakeupInterval_night;//单位秒
    String nowFrequency;
    int runMode = 1;              // 运行模式，高性能模式，省电模式，智能模式
    bool allowOfflineRun = false; // 是否允许离线运行
    String firmwareUrl;
    boolean isForceUpgrade; // 是否强制升级
    String firmwareVersionCode;
    String firmwareVersionName;

public:

    String getStartTime(){
        return startTime;
    }

    String getEndTime(){
        return endTime;
    }

    // long getWakeupInterval()
    // {
    //     if (!wakeupInterval.isEmpty() && (!wakeupInterval.equals("null")))
    //     {
    //         return wakeupInterval.toInt();
    //     }
    //     return getDefualtWakeupInterval();
    // }

    void setWakeupInterval(String interval)
    {
        this->wakeupInterval = interval;
    }

    bool getAllowOffLineRun()
    {
        return allowOfflineRun;
    }

    void setAllowOffLineRun(bool b)
    {
        allowOfflineRun = b;
    }

    long getFrequencyDaytime()
    {

      
        if (frequency_daytime != nullptr)
        {
            return frequency_daytime.toInt();
        }
        return 60 ;
    }

    long getNowFrequency(){
        if (nowFrequency != nullptr)
        {
            return nowFrequency.toInt();
        }
        return 60 ;  
    }
    

    void print()
    {

        printStr("frequency_daytime", frequency_daytime);
        printStr("frequency_night", frequency_night);
        printStr("wakeupInterval", wakeupInterval);
        printStr("allowOfflineRun", allowOfflineRun);
        printStr("runMode", runMode);
        printStr("wakeupInterval", wakeupInterval);
        printStr("isForceUpgrade", isForceUpgrade);

        printStr("wakeupInterval_daytime", wakeupInterval_daytime);
        printStr("wakeupInterval_night", wakeupInterval_night);




        // printStr("firmwareVersionCode",firmwareVersionCode);
        // printStr("firmwareVersionName",firmwareVersionName);

        // String endTime;
        // String startTime;
        // String frequency_daytime;
        // String frequency_night;
        // String wakeupInterval;
        // int runMode=1;//运行模式，高性能模式，省电模式，智能模式
        // bool allowOfflineRun=false;//是否允许离线运行
        // String firmwareUrl;
        // boolean isForceUpgrade;//是否强制升级
        // String firmwareVersionCode;
        // String firmwareVersionName;
    }


long getWakeupInterval(){
   // long epoch = getEpoch();
    
    if(isDayTime()){
        printStr("白天");
        if(isStringEmpty(wakeupInterval_daytime)){
                  printStr("白天空");
            return 60*5;
        }
        return wakeupInterval_daytime.toInt();
    }else{
        //  printStr("epoch",epoch);
          printStr("黑夜");
          if(isStringEmpty(wakeupInterval_night)){
              printStr("黑夜空");
            return 60*10;
        }
        return wakeupInterval_night.toInt();
    }
     
}

void setWakeupInterval_Daytime(String daytime){
    this->wakeupInterval_daytime = daytime;
}

void setWakeupInterval_Night(String nightTime){
    this->wakeupInterval_night = nightTime;
}


 
    void setRunMode(int mode)
    {
        this->runMode = mode;
    }

    void setUpdateFirmware(bool b)
    {
        this->isForceUpgrade = b;
    }

    bool getUpdateFirmware()
    {
        return this->isForceUpgrade;
    }

    String getVersionName()
    {
        return this->firmwareVersionName;
    }

    void setVersionName(String versionName)
    {
        this->firmwareVersionName = versionName;
    }

    void setVersion(String version)
    {
        this->firmwareVersionCode = version;
    }

    String getVersion()
    {
        return this->firmwareVersionCode;
    }

    String getFirmwareUrl()
    {
        return this->firmwareUrl;
    }

    void setFirmwareUrl(String url)
    {
        this->firmwareUrl = url;
    }

    int getRunMode()
    {
        return this->runMode;
    }

    void setEndTime(String endTime)
    {
        this->endTime = endTime;
    }

    void setStartTime(String startTime)
    {
        this->startTime = startTime;
    }

    void setFrequencyDaytime(String time)
    {
        this->frequency_daytime = time;
    }
    void setFrequencyNighttime(String time)
    {
        this->frequency_night = time;
    }

    TypeConfig()
    {
    }
    ~TypeConfig()
    {
    }
};

class LocalConfig
{
private:
    bool beep=true;
    bool indicatorLight=true;
    /* data */
    TypeConfig *typeConfig;

public:
    void print()
    {
        if(this->typeConfig!=nullptr){
            this->typeConfig->print();
        }
    }

    void  setBeep(bool b){
        this->beep = b;
    }

    bool getBeep(){
        return beep;
    }

   bool getIndicatorLight(){
        return this->indicatorLight;
    }

   void  setIndicatorLight(bool b){
        this->indicatorLight = b;
    }

    void setTypeConfig(TypeConfig *tf)
    {
        this->typeConfig = tf;
    }

    TypeConfig *getTypeConfig()
    {
        if(this->typeConfig==nullptr){
            this->typeConfig = new TypeConfig();
        }
        return this->typeConfig;
    }
    LocalConfig(/* args */)
    {
        this->typeConfig = new TypeConfig();
    }
    ~LocalConfig()
    {
        if (this->typeConfig != nullptr)
        {
            delete this->typeConfig;
        }
    }
};

// 假设你的类定义和构造函数保持不变（如上面的代码所示）

// 辅助函数，用于打印String数组
void printStringArray(String arr[], int count);

// 辅助函数，用于打印TimerActionA数组
void printTimerActionAArray(TimerActionA *arr[], int count);

// 辅助函数，用于打印TimerActionB数组
void printTimerActionBArray(TimerActionB *arr[], int count);
// 辅助函数，用于打印DayItem数组
void printDayItemArray(DayItem arr[], int count);

// 打印TimerAction对象
void printTimerAction(TimerAction &timerAction);
bool parseTimerAction(JsonDocument timerAction);
bool parseLocalConfig(JsonDocument localConfig);
bool parseTimerActionString(String timerAction);
bool parseLocalConfigString(String localConfig);
bool parseConfig(String config);
bool parseBTDevice(String btDevice);

void setLocalUpgrade(bool b);

#endif